package com.ljkj.blockpay.ui.activity;


import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.ljkj.blockpay.R;
import com.ljkj.blockpay.util.CodeUtils;
import com.ljkj.blockpay.widget.TitleView;
import com.tbruyelle.rxpermissions3.Permission;
import com.tbruyelle.rxpermissions3.RxPermissions;
import com.yxing.ScanCodeActivity;
import com.yxing.ScanCodeConfig;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


public class ScanActivity extends ScanCodeActivity {
    public final static int DEVICE_PHOTO_REQUEST = 1234;

    @Override
    public int getLayoutId() {
        return R.layout.activity_scan;
    }

    @Override
    public void initData() {
        super.initData();

        TitleView scantitle = findViewById(R.id.tv_title);
        scantitle.setLeftImgBtn(R.mipmap.dl_fanhui_night);
        scantitle.setRightText(R.string.album);
        scantitle.setRightColor(R.color.white);
        scantitle.setTitleContent(R.string.scan);
        scantitle.setTitleColor(R.color.white);
        scantitle.setLeftBackFinish(ScanActivity.this);
        scantitle.setRightTextListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new RxPermissions(ScanActivity.this).requestEachCombined(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Observer<Permission>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Permission permission) {
                        if (permission.granted){
                            Intent intent = new Intent(Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(intent, DEVICE_PHOTO_REQUEST);

                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == DEVICE_PHOTO_REQUEST) {
            if (data != null) {
                Uri uri = data.getData();
                ContentResolver cr = getContentResolver();
                try {
                    Bitmap mBitmap = MediaStore.Images.Media.getBitmap(cr, uri);//显得到bitmap图片
                    String s = ScanCodeConfig.scanningImage(this, uri);
                    Toast.makeText(this, s, Toast.LENGTH_SHORT).show();

                    if (mBitmap != null) {
                        mBitmap.recycle();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }}

}